<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Data-co-pilot 用户需求页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 100px auto;
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 4px;
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        p {
            line-height: 1.6;
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            min-height: 150px;
            resize: vertical;
        }

        .input-group textarea:focus {
            outline: none;
            border-color: #5cb85c;
            box-shadow: 0 0 8px rgba(92, 184, 92, 0.2);
        }

        button {
            padding: 10px 20px;
            background: #5cb85c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            display: block;
            margin: auto;
        }

        button:hover {
            background: #4cae4c;
        }

        #response {
            margin-top: 20px;
            white-space: pre-wrap;
            background: #f8f8f8;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>Welcome to Data-co-pilot</h2>
    <form id="nlp-form">
        <p>请描述你的需求，以便我们生成SQL语句。</p>
        <div class="input-group">
            <label>
                <textarea placeholder="在这里输入你的需求..." id="query" name="cmd"></textarea>
            </label>
        </div>
        <button type="submit">生成SQL语句</button>
    </form>
    <div>
        <p>注意事项：</p>
        <ul>
            <li>用户将需求清楚的表述</li>
            <li>尽量避免使用较为生僻的词语进行表述</li>
            <li>表达尽量简洁明了，通俗易懂</li>
        </ul>
    </div>
</div>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const returnFromIndex = localStorage.getItem('returnFromIndex');
        if (returnFromIndex === 'true') {
            const savedQuery = localStorage.getItem('userQuery');
            if (savedQuery) {
                document.getElementById('query').value = savedQuery;
            }
            localStorage.removeItem('returnFromIndex');
        }
    });

    document.getElementById('nlp-form').addEventListener('submit', function(event) {
        event.preventDefault();
        const query = document.getElementById('query').value;
        localStorage.setItem('userQuery', query); // Save the query to localStorage
        fetch('/nl_to_sql/', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ query })
        })
        .then(response => response.json())
        .then(data => {
            if (data.sql_query) {
                localStorage.setItem('sqlQuery', data.sql_query);
                window.location.href = '/index/';
            } else {
                document.getElementById('response').innerText = 'Error: ' + JSON.stringify(data);
            }
        })
        .catch(error => {
            document.getElementById('response').innerText = 'Error: ' + error;
        });
    });
</script>
</body>
</html>
